package 算法.线性表;

import java.util.Arrays;
import java.util.Queue;

/**
 * @author: 谢君臣
 * @create: 2020-12-26 16:36
 * @founction: XXXX
 */
public class 快速排序 {
    public static int[] array;
    public static void main(String[] args) {
        array = new int[]{4, 5, 3, 1, 8, 2, 9, 7, 0, 6, 11, 10};
        quickSort(array,0,array.length-1);
        System.out.println(Arrays.toString(array));
    }

    private static void quickSort(int[] array, int l, int r) {
        if (l >= r)
            return;
        else {
            int i = l, j = r, temp = array[i];
            while (i < j) {
                while (array[j] >= temp && i < j) {
                    j--;
                }
                while (array[i] <= temp && i < j) {
                    i++;
                }

                if (i != j)
                    swap(array, i, j);
            }
            swap(array, l, i);
            quickSort(array, l, i - 1);
            quickSort(array, i + 1, r);
        }
    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
